package mobi.greenplate.theweb.repository;

import mobi.greenplate.theweb.domain.Location;
import java.util.List;
import java.util.ArrayList;
import javax.annotation.security.RolesAllowed;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import mobi.greenplate.theweb.domain.User;
import mobi.greenplate.theweb.user.UserUtils;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository("locationRepository")
public class LocationRepository {

    private EntityManager entityManager;

    @PersistenceContext
    void setEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;
    }

    @RolesAllowed(value = {"ROLE_USER"})
    @Transactional
    public void delete(Long id) {
        final Location forDelete = entityManager.getReference(Location.class, id);
        entityManager.remove(forDelete);
    }

    @RolesAllowed(value = {"ROLE_USER"})
    @Transactional
    public Location save(Location location,User owner) {
        location.setOwner(owner);
        return entityManager.merge(location);
    }
    
    @RolesAllowed(value = {"ROLE_USER"})
    public Location findById(Long id) {
        final Location location = entityManager.find(Location.class, id);
        if(null==location){
            throw new IllegalArgumentException("location with id="+id+" not found");
        }
        return location;        
    }

    public int countAll() {
        return ((Integer) entityManager.createQuery("SELECT count(o) from Location o").
                getSingleResult()).intValue();
    }

    @RolesAllowed(value = {"ROLE_USER"})
    @Transactional
    public List<Location> findAll(int firstResult, int maxResult) {
        final List<Location> founded = entityManager.createQuery("SELECT o FROM Location o").
                setFirstResult(firstResult).
                setMaxResults(maxResult).getResultList();
        return new ArrayList<Location>(founded);
    }


    //ManyToOne by address
    public int countByAddress(Long addressId) {
        return ((Integer) entityManager.createQuery("SELECT count(o) from Location o WHERE o.addressId=:addressId").
                setParameter("addressId", addressId).
                getSingleResult()).intValue();
    }

    @RolesAllowed(value = {"ROLE_USER"})
    @Transactional
    public List<Location> findByAddress(int firstResult, int maxResult, Long addressId) {
        final List<Location> founded = entityManager.createQuery("SELECT o FROM Location o WHERE o.addressId=:addressId").
                setParameter("addressId", addressId).
                setFirstResult(firstResult).
                setMaxResults(maxResult).getResultList();
        return new ArrayList<Location>(founded);
    }    

    //ManyToOne by owner
    public int countByOwner(Long ownerId) {
        return ((Integer) entityManager.createQuery("SELECT count(o) from Location o WHERE o.ownerId=:ownerId").
                setParameter("ownerId", ownerId).
                getSingleResult()).intValue();
    }

    @RolesAllowed(value = {"ROLE_USER"})
    @Transactional
    public List<Location> findByOwner(int firstResult, int maxResult, Long ownerId) {
        final List<Location> founded = entityManager.createQuery("SELECT o FROM Location o WHERE o.ownerId=:ownerId").
                setParameter("ownerId", ownerId).
                setFirstResult(firstResult).
                setMaxResults(maxResult).getResultList();
        return new ArrayList<Location>(founded);
    }    


    
    
}
